package cn.e4j.modular.crm.service.impl;

import cn.e4j.core.dto.impl.Dto;
import cn.e4j.core.shiro.ShiroKit;
import cn.e4j.core.shiro.ShiroUser;
import cn.e4j.core.util.DateUtils;
import cn.e4j.core.util.LangUtils;
import cn.e4j.modular.crm.entity.GwajT;
import cn.e4j.modular.crm.entity.GwajlT;
import cn.e4j.modular.crm.mapper.GwajTMapper;
import cn.e4j.modular.crm.service.GwajTService;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author shixc
 * @since 2019-01-10
 */
@Service
public class GwajTServiceImpl extends ServiceImpl<GwajTMapper, GwajT> implements GwajTService {

    @Transactional(readOnly = true)
    public List<Map<String, Object>> getList(Page<Object> page, Dto dto) {

        List<Map<String, Object>> gwajDtos = this.baseMapper.listPage(page, dto);

        return gwajDtos;
    }

    @Transactional(readOnly = true)
    public GwajT getByKey(String GWAJENT, String GWAJ001, String GWAJ002) {
        GwajT gwaj = this.baseMapper.getByKey(GWAJENT, GWAJ001, GWAJ002);
        return gwaj;
    }

    @Transactional
    @Override
    public void add(Dto dto) {
        ShiroUser shiroUser = ShiroKit.getUser();
        GwajT gwaj = new GwajT();
        gwaj.setGWAJ001(dto.getString("GWAJ001"));
        gwaj.setGWAJENT(shiroUser.getDBC());
        gwaj.setGWAJACTI(dto.getString("GWAJACTI"));
        gwaj.setGWAJ002(dto.getString("GWAJ002"));
        gwaj.setGWAJ003(" ");
        gwaj.setGWAJ004(" ");
        gwaj.setGWAJUSER(shiroUser.getACCOUNT());
        gwaj.setGWAJGRUP(shiroUser.getORG_ID());
        gwaj.setGWAJDATE(LocalDate.now());
        gwaj.setGWAJTIME(DateUtils.formatDate(new Date(), "HH:mm:ss"));
        gwaj.setGWAJORIG(shiroUser.getORG_ID());
        gwaj.setGWAJORIU(shiroUser.getACCOUNT());
        GwajlT gwajl = new GwajlT();
        gwajl.setGWAJLENT(shiroUser.getDBC());
        gwajl.setGWAJL001(dto.getString("GWAJ001"));
        gwajl.setGWAJL002(dto.getString("GWAJ002"));
        gwajl.setGWAJL003(LangUtils.getLang(shiroUser.getLANG()));
        gwajl.setGWAJL004(dto.getString("GWAJL004"));
        gwajl.setGWAJL005(dto.getString("GWAJL005"));
        gwaj.insert();
        gwajl.insert();
    }

    @Transactional
    @Override
    public void edit(Dto dto) {
        ShiroUser shiroUser = ShiroKit.getUser();
        GwajT gwaj = new GwajT();
        gwaj.setGWAJACTI(dto.getString("GWAJACTI"));
        gwaj.setGWAJMODU(shiroUser.getACCOUNT());
        UpdateWrapper<GwajT> gwajTUpdateWrapper = new UpdateWrapper<>();
        gwajTUpdateWrapper.eq("GWAJENT", shiroUser.getDBC());
        gwajTUpdateWrapper.eq("GWAJ002", dto.getString("GWAJ002"));

        GwajlT gwajl = new GwajlT();
        gwajl.setGWAJL004(dto.getString("GWAJL004"));
        gwajl.setGWAJL005(dto.getString("GWAJL005"));
        UpdateWrapper<GwajlT> gwajlTUpdateWrapper = new UpdateWrapper<>();
        gwajlTUpdateWrapper.eq("GWAJLENT", shiroUser.getDBC());
        gwajlTUpdateWrapper.eq("GWAJL002", dto.getString("GWAJ002"));

        gwaj.update(gwajTUpdateWrapper);
        gwajl.update(gwajlTUpdateWrapper);
    }


    @Transactional
    @Override
    public void delete(Dto dto) {
        ShiroUser shiroUser = ShiroKit.getUser();
        GwajT gwaj = new GwajT();
        UpdateWrapper<GwajT> gwajTUpdateWrapper = new UpdateWrapper<>();
        gwajTUpdateWrapper.eq("GWAJENT", shiroUser.getDBC());
        gwajTUpdateWrapper.eq("GWAJ001", dto.getString("GWAJ001"));
        gwajTUpdateWrapper.eq("GWAJ002", dto.getString("GWAJ002"));
        GwajlT gwajl = new GwajlT();
        UpdateWrapper<GwajlT> gwajlTUpdateWrapper = new UpdateWrapper<>();
        gwajlTUpdateWrapper.eq("GWAJLENT", shiroUser.getDBC());
        gwajlTUpdateWrapper.eq("GWAJL001", dto.getString("GWAJ001"));
        gwajlTUpdateWrapper.eq("GWAJL002", dto.getString("GWAJ002"));
        gwaj.delete(gwajTUpdateWrapper);
        gwajl.delete(gwajlTUpdateWrapper);
    }

    @Transactional
    @Override
    public void delDataBatch(List<GwajT> gwajTList) {
        ShiroUser shiroUser = ShiroKit.getUser();
        for (GwajT gwajT : gwajTList) {
            UpdateWrapper<GwajT> gwajTUpdateWrapper = new UpdateWrapper<>();
            gwajTUpdateWrapper.eq("GWAJENT", shiroUser.getDBC());
            gwajTUpdateWrapper.eq("GWAJ001", gwajT.getGWAJ001());
            gwajTUpdateWrapper.eq("GWAJ002", gwajT.getGWAJ002());
            GwajlT gwajl = new GwajlT();
            UpdateWrapper<GwajlT> gwajlTUpdateWrapper = new UpdateWrapper<>();
            gwajlTUpdateWrapper.eq("GWAJLENT", shiroUser.getDBC());
            gwajlTUpdateWrapper.eq("GWAJL001", gwajT.getGWAJ001());
            gwajlTUpdateWrapper.eq("GWAJL002", gwajT.getGWAJ002());
            gwajT.delete(gwajTUpdateWrapper);
            gwajl.delete(gwajlTUpdateWrapper);
        }
    }

    @Transactional(readOnly = true)
    public List<Map<String, Object>> getGwaiList(Dto dto) {

        List<Map<String, Object>> gwaiDtos = this.baseMapper.getGwaiList(dto);

        return gwaiDtos;
    }
}
